package com.ims.shiro;

import com.ims.redis.RedisCacheSessionDao;
import com.ims.redis.RedisManager;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * 直接操作Session属性，不会被保存
 * 封装Session属性相关操作 Session属性发生改变时保存到Redis中并通知其它节点清空本地EhCache缓存
 */
public class ShiroSessionService {

    private Logger logger = LoggerFactory.getLogger(ShiroSessionService.class);

    @Resource
    RedisCacheSessionDao redisCacheSessionDao;

    @Resource
    RedisManager redisManager;

    @Resource
    RedisTemplate<Serializable, Object> redisTemplate;
    private  long seconds = 0;
    public void setSeconds(long seconds) {
        this.seconds = seconds;
    }


    public ShiroSession getSession() throws Exception {
        if (SecurityUtils.getSubject().getSession() != null)
        {
            redisManager.setRedisTemplate(redisTemplate);
            redisCacheSessionDao.setRedisManager(redisManager);
            redisCacheSessionDao.setSeconds(seconds);
            return (ShiroSession) this.redisCacheSessionDao.readSession(SecurityUtils.getSubject().getSession().getId());
        }
        else{
            throw new Exception("session被清除");
        }

    }


    public void setId(Serializable id) throws Exception {
        ShiroSession session = this.getSession();
        session.setId(id);
        this.redisCacheSessionDao.update(session);


    }

    public void setStopTimestamp(Date stopTimestamp) throws Exception{
        ShiroSession session = this.getSession();
        session.setStopTimestamp(stopTimestamp);
        this.redisCacheSessionDao.update(session);

    }

    public void setExpired(boolean expired) throws Exception{
        ShiroSession session = this.getSession();
        session.setExpired(expired);
        this.redisCacheSessionDao.update(session);

    }

    public void setTimeout(long timeout) throws Exception{
        ShiroSession session = this.getSession();
        session.setTimeout(timeout);
        this.redisCacheSessionDao.update(session);

    }

    public void setHost(String host) throws Exception{
        ShiroSession session = this.getSession();
        session.setHost(host);
        this.redisCacheSessionDao.update(session);

    }

    public void setAttributes(Map<Object, Object> attributes) throws Exception{
        ShiroSession session = this.getSession();
        session.setAttributes(attributes);
        this.redisCacheSessionDao.update(session);

    }

    public Map<Object, Object> getAttributes() throws Exception{
        return this.getSession().getAttributes();
    }

    public void setAttribute(Object key, Object value) throws Exception{
        ShiroSession session = this.getSession();
        session.setAttribute(key, value);
        this.redisCacheSessionDao.update(session);

    }

    public Object getAttribute(Object key) throws Exception{
        return this.getSession().getAttribute(key);
    }

    public Collection<Object> getAttributeKeys() throws Exception{
        return this.getSession().getAttributeKeys();
    }

    public Object removeAttribute(Object key) throws Exception{
        ShiroSession session = this.getSession();
        Object res = session.removeAttribute(key);
        this.redisCacheSessionDao.update(session);

        return res;
    }


}
